//简单框体结构
package using.supers;//框体
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;

//查询界面
class apply extends JFrame { public int i=0;
    public apply() { setTitle("Application interface");
        Container container=getContentPane();
        container.setLayout(new GridLayout(4,1));
        JPanel panel1=new JPanel();
        setBounds(240,240,1000,800);
        JLabel jl1 = new JLabel("Activity name");
        JTextField jt1=new JTextField();
        JButton jb1=new JButton("find");
        JButton jb2=new JButton("update");
        JButton jb3=new JButton("delete");
        JButton jb4=new JButton("new");
        panel1.setBorder(BorderFactory.createTitledBorder("Management interface"));
        panel1.setLayout(null);
        JLabel jl2=new JLabel(" Application No");
        JLabel jl3=new JLabel(" Activity name");
        JLabel jl4 = new JLabel("Activity time:");
        JLabel jl5 = new JLabel("applicant");
        JLabel jl6 = new JLabel("Audit status");
        JTextField jt2 = new JTextField();
        JTextField jt3 = new JTextField();
        JTextField jt4 = new JTextField();
        JTextField jt5 = new JTextField();
        JTextField jt6 = new JTextField();
        jl1.setBounds(50, 100, 70, 30);
        jt1.setBounds(110, 100, 70, 30);
        jb1.setBounds(200, 100, 70, 30);
        jb2.setBounds(590, 40, 70, 30);
        jb3.setBounds(590, 100, 70, 30);
        jb4.setBounds(590, 160, 70, 30);
        jl2.setBounds(300, 40, 70, 30);
        jt2.setBounds(360, 40, 70, 30);
        jl3.setBounds(300, 90, 70, 30);
        jt3.setBounds(360, 90, 70, 30);
        jl4.setBounds(300, 140, 70, 30);
        jt4.setBounds(360, 140, 70, 30);
        jl5.setBounds(450, 40, 70, 30);
        jt5.setBounds(500, 40, 70, 30);
        jl6.setBounds(440, 90, 70, 30);
        jt6.setBounds(500, 90, 70, 30);
        panel1.add(jl1);panel1.add(jt1);panel1.add(jb1);panel1.add(jb2);
        panel1.add(jb3);panel1.add(jb4);panel1.add(jl2);panel1.add(jl3);
        panel1.add(jl4);panel1.add(jl5);panel1.add(jl6);panel1.add(jt2);
        panel1.add(jt3);panel1.add(jt4);panel1.add(jt5);panel1.add(jt6);
        jb1.addActionListener(new ActionListener() {@Override
            public void actionPerformed(ActionEvent e) {
                SQL_sever testmysql = new SQL_sever();
                try {
                    String str1 = "select * from [apply] where ano='" + jt1.getText() + "'";
                    String str2, str3, str4, str5, str6;
                    SQL_sever.sql = SQL_sever.con.createStatement();
                    SQL_sever.res = SQL_sever.sql.executeQuery(str1);
                    int number = 0;
                    while (SQL_sever.res.next()) {
                        str2 = SQL_sever.res.getString(1);
                        str3 = SQL_sever.res.getString(2);
                        str4 = SQL_sever.res.getString(3);
                        str5 = SQL_sever.res.getString(4);
                        str6 = SQL_sever.res.getString(5);
                        number++;jt2.setText(str2);jt3.setText(str3);
                        jt4.setText(str4);jt5.setText(str5);jt6.setText(str6); }
                    SQL_sever.res.absolute(0);// 移动指针
                    String[][] tables = new String[number][5];
                    i = 0;
                    while (SQL_sever.res.next()) { int j = 0;
                        tables[i][j++] = SQL_sever.res.getString(1);
                        tables[i][j++] = SQL_sever.res.getString(2);
                        tables[i][j++] = SQL_sever.res.getString(3);
                        tables[i][j++] = SQL_sever.res.getString(4);
                        tables[i++][j++] = SQL_sever.res.getString(5);
                        tables[i++][j++] = SQL_sever.res.getString(6);
                        number++; }
                    String[] column = { "Application No", "Activity name", "Activity time", "applicant", "Audit status" };
                    JTable testtable = new JTable(tables, column);
                    testtable.setSelectionBackground(Color.yellow);
                    JScrollPane scrollPane = new JScrollPane(testtable);
                    scrollPane.setBorder(BorderFactory.createTitledBorder("Activity application information"));
                    container.add(scrollPane, BorderLayout.CENTER);
                    validate();
                } catch (SQLException e1) { e1.printStackTrace(); } }});
        jb2.addActionListener(new ActionListener() {@Override
            public void actionPerformed(ActionEvent e) { String str1, str2, str3, str4, str5;
                str1 = jt2.getText();
                str2 = jt3.getText();
                str3 = jt4.getText();
                str4 = jt5.getText();
                str5 = jt6.getText();
                String str6 = "update [apply] set ano=?,aname=?,atime=?,apeople=?,astate=?where aname='" + jt1.getText() + "'";
                try { SQL_sever.con = SQL_sever.connection();
                    PreparedStatement sql = SQL_sever.con.prepareStatement(str6);
                    sql.setString(1, str1);
                    sql.setString(2, str2);
                    sql.setString(3, str3);
                    sql.setString(4, str4);
                    sql.setString(5, str5);
                    int result = sql.executeUpdate();
                    validate();
                } catch (SQLException e1) { e1.printStackTrace(); }
                String[] column = { "Application No", "Activity name", "Activity time", "applicant", "Audit status" };
                String[][] tables = { { str1, str2, str3, str4, str5 } };
                JTable table = new JTable(tables, column);
                JScrollPane scrollpane2 = new JScrollPane(table);
                scrollpane2.setBorder(BorderFactory.createTitledBorder("After update"));
                container.add(scrollpane2, BorderLayout.CENTER);
                JOptionPane.showMessageDialog(null, "Update succeeded");
                validate(); }});
        jb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str1 = jt2.getText();
                String str2 = "delete from [apply] where ano='" + str1 + "'";
                System.out.println(str2);
                try { SQL_sever.con = SQL_sever.connection();
                    PreparedStatement sql = SQL_sever.con.prepareStatement(str2);
                    int result = sql.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Delete succeeded");
                } catch (SQLException e1) { e1.printStackTrace(); } }});
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str3, str4, str5, str6, str8 = null;
                String str1 = jt2.getText();
                String str2 = jt3.getText();
                str3 = jt4.getText();
                str4 = jt5.getText();
                str5 = jt6.getText();
                String str7 = "select ano from [apply] where ano='" + str1 + "'";
                if (str1.equals("") || str2.equals("") || str3.equals("") || str4.equals("") || str5.equals("")) {
                    JOptionPane.showMessageDialog(null, "All information cannot be empt"); }
                else
                { try { SQL_sever.sql = SQL_sever.con.createStatement();
                        SQL_sever.res = SQL_sever.sql.executeQuery(str7);
                        while (SQL_sever.res.next()) {
                            str8 = SQL_sever.res.getString("ano"); }
                        if (str8 == null) {
                            str8 = "insert into [apply]values(?,?,?,?,?)";
                            PreparedStatement sql = SQL_sever.con.prepareStatement(str8);
                            sql.setString(1, str1);
                            sql.setString(2, str2);
                            sql.setString(3, str3);
                            sql.setString(4, str4);
                            sql.setString(5, str5);
                            int result = sql.executeUpdate();
                            JOptionPane.showMessageDialog(null, "Insert successful"); } else {
                            JOptionPane.showMessageDialog(null, "Application number already exists"); }
                    } catch (SQLException e1) { e1.printStackTrace(); } } }});
        container.add(panel1);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true); }
    public static void tan() {
        apply student = new apply();
        student.setVisible(true); }}